package cn.nyse.ktc_user.controller;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import cn.nyse.ktc_user.entity.User;
import cn.nyse.ktc_user.service.UserService;

import wfw.cn.ktc_common.entity.PageResult;
import wfw.cn.ktc_common.entity.Result;
import wfw.cn.ktc_common.entity.ResultCode;
import wfw.cn.ktc_common.unit.JwtUtil;

/**
 * @Description 用户 控制器层
 * @author admin
 * @date 2020-10-14 17:34:43
*/
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController{

    @Autowired
    private UserService userService;

    @Autowired
    JwtUtil jwtUtil;

    @GetMapping("/one/{id}")
    public Result findo(@PathVariable String id){
        return new Result(ResultCode.SUCCESS, userService.findo(id));
    }

    @PostMapping("")
    public Result insertUser(@RequestBody User user){
        userService.insertUser(user);
        return new Result(ResultCode.SUCCESS);
    }

    @GetMapping("")
    public Result users(){
        return new Result(ResultCode.SUCCESS,userService.findAll());
    }


    @PostMapping("/login")
    public Result login(@RequestBody User user){
        User login = userService.login(user.getMobile(), user.getPassword());
        if (login==null){
            return new Result(ResultCode.USER_PASSWORD_ERROR);
        }
        String token = jwtUtil.createJWT(user.getId(), user.getMobile(), "user");
        HashMap<String, String> map = new HashMap<>();
        map.put("token",token);
        return new Result(ResultCode.SUCCESS,map);
    }

    @PostMapping("/register/{code}")
    public Result register(@RequestBody User user,@PathVariable String code){
        userService.register(user);
        return new Result(ResultCode.SUCCESS);
    }

    @GetMapping("/{userId}")
    public Result findByUserId(@PathVariable String userId){
        return new Result(ResultCode.SUCCESS,userService.findById(userId));
    }

    @PutMapping("/{userId}")
    public Result updateOne(@PathVariable String userId,@RequestBody User user){
        user.setId(userId);
        user.setUpdatedate(new Date());
        userService.update(user);
        return new Result(ResultCode.SUCCESS);
    }

    @RequestMapping(value = "/updateFollowCount/{userid}/{count}", method = RequestMethod.PUT)
    public Result updateFollowCount(@PathVariable String userid,@PathVariable Integer count) {

        userService.updateFollowCount(userid,count);

        return new Result(ResultCode.SUCCESS);
    }

    @RequestMapping(value = "/updateFansCount/{userid}/{count}", method = RequestMethod.PUT)
    public Result updateFansCount(@PathVariable String userid,@PathVariable Integer count) {
        userService.updateFansCount(userid,count);
        return new Result(ResultCode.SUCCESS);
    }

    @DeleteMapping("/{userId}")
    public Result deleteUser(@PathVariable String userId){
        userService.deleteById(userId);
        return new Result(ResultCode.SUCCESS);
    }

    @GetMapping("/info")
    public Result userInfo(){
        return null;
    }

    @PutMapping("/saveinfo")
    public Result saveInfo(@RequestBody User user){
        return null;
    }

    @PostMapping("/search/{page}/{size}")
    public Result searchPage(@PathVariable("page")int page,@PathVariable("size")int size){
        Page<User> users = userService.findPage(page, size);
        return new Result(ResultCode.SUCCESS,new PageResult<User>(users.getTotalElements(),users.getContent()));
    }

    @GetMapping("sms/{mobile}")
    public Result sms(@PathVariable String mobile){
        userService.sendSms(mobile);
        return new Result(ResultCode.SUCCESS);
    }



}
